package com.ruoyi.wq.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.Condition;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.ip.Address;
import com.ruoyi.common.utils.ip.AddressUtils;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.wq.domain.WqDynamic;
import com.ruoyi.wq.domain.WqDynamicTopic;
import com.ruoyi.wq.domain.WqMiniGame;
import com.ruoyi.wq.domain.vo.WqDynamicVO;
import com.ruoyi.wq.mapper.WqDynamicMapper;
import com.ruoyi.wq.mapper.WqMiniGameMapper;
import com.ruoyi.wq.service.IWqDynamicService;
import com.ruoyi.wq.service.IWqDynamicTopicService;
import com.ruoyi.wq.service.IWqTopicService;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.RandomUtil;

/**
 * 动态Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-03-12
 */
@Service
public class WqDynamicServiceImpl extends ServiceImpl<WqDynamicMapper, WqDynamic> implements IWqDynamicService {
	
    @Autowired
    private WqDynamicMapper wqDynamicMapper;
    
    @Autowired
    private IWqDynamicTopicService wqDynamicTopicService;
    
    @Autowired
    private WqMiniGameMapper wqMiniGameMapper;
    
    @Autowired
    private ISysUserService sysUserService;
    
    @Autowired
    private IWqTopicService wqTopicService;
    
    @Autowired
	private RedisCache redisCache;
    
    public final static String KEY_DYNAMIC = "key:dynamic:";
    public final static String KEY_DYNAMIC_TOTAL = "key:dynamic:total:";
    
    /**
     * 查询动态
     * 
     * @param id 动态主键
     * @return 动态
     */
    @Override
    public WqDynamic selectWqDynamicById(String id)
    {
        return wqDynamicMapper.selectWqDynamicById(id);
    }

    /**
     * 查询动态列表
     * 
     * @param wqDynamic 动态
     * @return 动态
     */
    @Override
    public List<WqDynamic> selectWqDynamicList(WqDynamic wqDynamic)
    {
        return wqDynamicMapper.selectWqDynamicList(wqDynamic);
    }

    /**
     * 新增动态
     * 
     * @param wqDynamic 动态
     * @return 结果
     */
    @Override
    public int insertWqDynamic(WqDynamic wqDynamic)
    {
        wqDynamic.setCreateTime(DateUtils.getNowDate());
        return wqDynamicMapper.insertWqDynamic(wqDynamic);
    }

    /**
     * 修改动态
     * 
     * @param wqDynamic 动态
     * @return 结果
     */
    @Override
    public int updateWqDynamic(WqDynamic wqDynamic)
    {
        wqDynamic.setUpdateTime(DateUtils.getNowDate());
        return wqDynamicMapper.updateWqDynamic(wqDynamic);
    }

    /**
     * 批量删除动态
     * 
     * @param ids 需要删除的动态主键
     * @return 结果
     */
    @Override
    public int deleteWqDynamicByIds(String[] ids)
    {
        return wqDynamicMapper.deleteWqDynamicByIds(ids);
    }

    /**
     * 删除动态信息
     * 
     * @param id 动态主键
     * @return 结果
     */
    @Override
    public int deleteWqDynamicById(String id)
    {
        return wqDynamicMapper.deleteWqDynamicById(id);
    }

	@Override
	public WqDynamicVO getDetail(WqDynamicVO dynamic) {
		WqDynamic wqDynamic = wqDynamicMapper.selectById(dynamic.getId());
		WqDynamicVO wqDynamicVO = new WqDynamicVO();
		BeanUtils.copyProperties(wqDynamic, wqDynamicVO);
		wqDynamicVO.setTime(DateUtils.getTime(wqDynamicVO.getCreateTime()));
		String topicIds = wqDynamic.getTopicIds();
		if(StringUtils.isNotBlank(topicIds)){
			List<WqDynamicTopic> topicList = wqDynamicTopicService.getTopicListByDynamicId(dynamic.getId());
			wqDynamicVO.setTopicList(topicList);
		}
		
		SysUser user = sysUserService.getById(wqDynamic.getUserId());
		if("0".equals(user.getWechatStatus()) && StringUtils.isNotBlank(user.getWechat())){
			wqDynamicVO.setWechatFlag(true);
		}else{
			wqDynamicVO.setWechatFlag(false);
		}
		
		wqDynamicVO.setIsManager("0");
		if(dynamic.getViewUserId() != null){
			SysUser viewUser = sysUserService.getById(dynamic.getViewUserId());
			if(viewUser != null){
				wqDynamicVO.setIsManager(viewUser.getIsManager());
			}
		}
		wqDynamicVO.setIsPay(user.getIsPay());
		return wqDynamicVO;
	}

	@Override
	public TableDataInfo getPageList(WqDynamicVO wqDynamic) {
		if(StringUtils.isBlank(wqDynamic.getLon())){
			Address address = AddressUtils.getAddressByIP();
			wqDynamic.setLon(address.getLng());
			wqDynamic.setLat(address.getLat());
		}
		
		IPage<WqDynamicVO> pageBean = Condition.getPage(wqDynamic);
		
		Map<String,WqDynamicTopic> topicMap = wqTopicService.getTopicMap();
		IPage<WqDynamicVO> page = baseMapper.selectPageList(pageBean, wqDynamic);
        for(WqDynamicVO vo : page.getRecords()){
        	if(vo.getIsParty().equals("0")){
        		String topicIds = vo.getTopicIds();
        		if(StringUtils.isNotBlank(topicIds)){
        			WqDynamicTopic topic = topicMap.get(topicIds);
        			List<WqDynamicTopic> topicList = new ArrayList<>();
        			topicList.add(topic);
        			vo.setTopicList(topicList);
        		}
        		vo.setTime(DateUtils.getTime(vo.getCreateTime()));
        		vo.setCoinLevel(StringUtils.getCoinLevel(vo.getPayNum()));
        		vo.setViewFlag(true);
        	}else if(vo.getIsParty().equals("2")){
        		WqMiniGame game = wqMiniGameMapper.selectById(vo.getUserId());
        		vo.setAvatar(game.getLogo());
        		vo.setNickName(game.getName());
        		vo.setStar(game.getStar());
        		vo.setPlayNum(game.getPlayNum());
        		vo.setMessage(game.getIntro());
        		vo.setImage(game.getImage());
        		vo.setTime(DateUtils.getTime(vo.getCreateTime()));
        		vo.setViewFlag(true);
        	}
        }
        
        return Condition.getTableDataInfo(page.getRecords(), page.getTotal());
	}

	@Override
	public TableDataInfo getMyPageList(WqDynamic wqDynamic) {
		QueryWrapper<WqDynamic> qw = Condition.getQueryWrapper(wqDynamic);
		wqDynamic.setUserId(SecurityUtils.getUserId());
        qw.setEntity(wqDynamic);
        qw.orderByDesc("create_time");
        IPage<WqDynamic> page = baseMapper.selectPage(Condition.getPage(wqDynamic), qw);
        List<WqDynamicVO> wqDynamicVOList = new ArrayList<WqDynamicVO>();
        for(WqDynamic vo : page.getRecords()){
        	WqDynamicVO wqDynamicVO = new WqDynamicVO();
        	BeanUtils.copyProperties(vo, wqDynamicVO);
        	String topicIds = vo.getTopicIds();
    		if(StringUtils.isNotBlank(topicIds)){
    			List<WqDynamicTopic> topicList = wqDynamicTopicService.getTopicListByDynamicId(vo.getId());
    			wqDynamicVO.setTopicList(topicList);
    		}
    		wqDynamicVO.setTime(DateUtils.getTime(wqDynamicVO.getCreateTime()));
    		wqDynamicVO.setViewFlag(true);
    		wqDynamicVOList.add(wqDynamicVO);
        }
        return Condition.getTableDataInfo(wqDynamicVOList, page.getTotal());
	}

	@Override
	public TableDataInfo getPageListByUserId(WqDynamic wqDynamic) {
		QueryWrapper<WqDynamic> qw = Condition.getQueryWrapper(wqDynamic);
		wqDynamic.setStatus("0");
        qw.setEntity(wqDynamic);
        qw.orderByDesc("create_time");
        IPage<WqDynamic> page = baseMapper.selectPage(Condition.getPage(wqDynamic), qw);
        List<WqDynamicVO> wqDynamicVOList = new ArrayList<WqDynamicVO>();
        for(WqDynamic vo : page.getRecords()){
        	WqDynamicVO wqDynamicVO = new WqDynamicVO();
        	BeanUtils.copyProperties(vo, wqDynamicVO);
        	String topicIds = vo.getTopicIds();
    		if(StringUtils.isNotBlank(topicIds)){
    			List<WqDynamicTopic> topicList = wqDynamicTopicService.getTopicListByDynamicId(vo.getId());
    			wqDynamicVO.setTopicList(topicList);
    		}
    		wqDynamicVO.setTime(DateUtils.getTime(wqDynamicVO.getCreateTime()));
    		wqDynamicVO.setViewFlag(true);
    		wqDynamicVOList.add(wqDynamicVO);
        }
        return Condition.getTableDataInfo(wqDynamicVOList, page.getTotal());
	}

	@Override
	public void updateDynamicPlan() {
		Integer size = RandomUtil.randomInt(1, 5);
		IPage<WqDynamic> page = new Page<>(1, size);
		QueryWrapper<WqDynamic> qw = new QueryWrapper<>();
		WqDynamic entity = new WqDynamic();
        qw.setEntity(entity);
        qw.orderByAsc("update_time");
        List<WqDynamic> dynamics = baseMapper.selectPage(page, qw).getRecords();
        for(WqDynamic dynamic : dynamics){
        	dynamic.setUpdateTime(DateUtil.date());
        	baseMapper.updateById(dynamic);
        }
	}

	@Override
	public Integer getNumByUserId(Long userId) {
		QueryWrapper<WqDynamic> queryWrapper = new QueryWrapper<>();
		WqDynamic entity = new WqDynamic();
		entity.setUserId(userId);
		queryWrapper.setEntity(entity);
		return baseMapper.selectCount(queryWrapper);
	}

	@Override
	public void addBlackByUserId(Long userId) {
		WqDynamic entity = new WqDynamic();
		entity.setStatus("1");
		entity.setDelFlag("1");
		
		UpdateWrapper<WqDynamic> updateWrapper = new UpdateWrapper<>();
		WqDynamic dynamic = new WqDynamic();
		dynamic.setUserId(userId);
		updateWrapper.setEntity(dynamic);
		baseMapper.update(entity, updateWrapper);
	}

	@Override
	public WqDynamicVO getGameDetail(String id) {
		WqDynamic wqDynamic = wqDynamicMapper.selectById(id);
		WqDynamicVO wqDynamicVO = new WqDynamicVO();
		BeanUtils.copyProperties(wqDynamic, wqDynamicVO);
		wqDynamicVO.setTime(DateUtils.getTime(wqDynamicVO.getCreateTime()));
		WqMiniGame game = wqMiniGameMapper.selectById(wqDynamicVO.getUserId());
		Long eyeNum = game.getEyeNum();
		game.setEyeNum(eyeNum+1);
		wqMiniGameMapper.updateById(game);
		
		wqDynamicVO.setAvatar(game.getLogo());
		wqDynamicVO.setNickName(game.getName());
		wqDynamicVO.setStar(game.getStar());
		wqDynamicVO.setPlayNum(game.getPlayNum());
		wqDynamicVO.setMessage(game.getIntro());
		wqDynamicVO.setImage(game.getImage());
		return wqDynamicVO;
	}

	@Override
	public void addBlackUserByUserId(Long userId) {
		WqDynamic entity = new WqDynamic();
		entity.setStatus("1");
		entity.setDelFlag("1");
		UpdateWrapper<WqDynamic> updateWrapper = new UpdateWrapper<>();
		WqDynamic dynamic = new WqDynamic();
		dynamic.setUserId(userId);
		updateWrapper.setEntity(dynamic);
		baseMapper.update(entity, updateWrapper);
	}

}
